const mysql = require('mysql');

const pool = mysql.createPool({
    // host: 'localhost',
    // user: 'root',
    // password: 'root',
    host: '47.106.117.200',
    user: 'yxz',
    password: 'loveisgod',
    database: 'lessonplan_class',
    // connectionLimit: 20 * 1000,
    // connectTimeout: 20 * 1000,
    acquireTimeout: 20 * 1000,      // 执行查询的时限
    timeout: 20 * 1000,             // ？
    multipleStatements: true,       // 允许同时执行多条语句。比如：批量更新
    port: 3306
});

const query = function(sql, callback) {
    pool.getConnection(function(err, conn) {
        if (err) {
            callback(err, null, null);
        } else {
            conn.query(sql, function(qerr, vals, fields) {
                conn.release(); // 释放连接
                callback(qerr, vals, fields); // 事件驱动回调
            });
        }
    });
};

module.exports = query;